"""辩论式法律思维训练路由"""
from fastapi import APIRouter, HTTPException, Body
from typing import Dict, Any, Optional, List
from app.services.debate_service import debate_service

router = APIRouter()


@router.post("/debate/start")
async def start_debate(
    user_id: str = Body(...),
    question_id: str = Body(...),
    question_content: str = Body(...),
    user_position: str = Body("中立"),
    question_options: Optional[List[str]] = Body(None)
):
    """开始辩论"""
    try:
        session = await debate_service.start_debate(
            user_id,
            question_id,
            question_content,
            user_position,
            question_options
        )
        return session
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/debate/submit-argument")
async def submit_argument(
    debate_id: str = Body(...),
    user_argument: str = Body(...),
    round_number: Optional[int] = Body(None)
):
    """提交用户论点"""
    try:
        result = await debate_service.submit_argument(
            debate_id,
            user_argument,
            round_number
        )
        return result
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/debate/ai-response")
async def get_ai_response(
    debate_id: str = Body(...),
    user_argument: str = Body(...)
):
    """获取AI回应（不更新轮次）"""
    try:
        response = await debate_service.get_ai_response(
            debate_id,
            user_argument
        )
        return response
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/debate/evaluate")
async def evaluate_debate(
    debate_id: str = Body(...)
):
    """评估辩论质量"""
    try:
        evaluation = await debate_service.evaluate_debate(debate_id)
        return evaluation
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/debate/end")
async def end_debate(
    debate_id: str = Body(...)
):
    """结束辩论"""
    try:
        result = await debate_service.end_debate(debate_id)
        return result
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/debate/{debate_id}")
async def get_debate_session(debate_id: str):
    """获取辩论会话信息"""
    try:
        from app.services.debate_service import debate_service
        session = await debate_service._get_debate_session(debate_id)
        if not session:
            raise HTTPException(status_code=404, detail="辩论会话不存在")
        return session
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


